﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace AUV5.Web.Pages
{
    public partial class UCWorkShare : System.Web.UI.UserControl
    {
        public Model.WorksShare DataSource { get; set; }

        public bool IsCover { get; set; }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            InitData();
        }

        protected void InitData()
        {
            if (DataSource != null)
            {
                Model.WorksShare share = DataSource;
                Model.UserInfo seller = BLL.UserInfoBLL.Instance.GetDetail(share.UserID);
                Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(share.ServiceID);

                if (IsCover)
                {
                    imgShare.CssClass = "cover";
                }

                lbDescription.Text = share.Description;

                if (service != null)
                {
                    lbServiceTitle.Text = service.Title;
                    lbServiceTitle.ToolTip = service.Title;

                    Model.ServiceCategory category = BLL.ServiceCategoryBLL.Instance.GetDetail(service.CategoryID);
                    if (category != null)
                    {
                        hlCategory.Text = category.CategoryName;
                        hlCategory.NavigateUrl = string.Format("/Pages/ShareList.aspx?category={0}", category.CategoryID);

                        categoryIcon.Style.Add("background", category.Color);
                    }
                }

                if (seller != null)
                {
                    lbUserName.Text = seller.UserName;
                    imgUser.ImageUrl = seller.PhotoPath;
                }
                if (share.AttachFormat == (int)Common.Enum.AttachFormat.Image)
                {
                    if (IsCover)
                    {
                        imgShare.ImageUrl = BLL.WorksShareBLL.Instance.GetImageCover(share.AttachPath);
                        hlShareImage.NavigateUrl = "/Pages/ShareList.aspx";
                    }
                    else
                    {
                        imgShare.ImageUrl = share.AttachPath;

                        string previewpath = BLL.WorksShareBLL.Instance.GetImagePreview(share.AttachPath);
                        if (!File.Exists(Server.MapPath(previewpath)))
                        {
                            previewpath = share.AttachPath;
                        }

                        hlShareImage.NavigateUrl = previewpath;
                    }
                }
                else if (share.AttachFormat == (int)Common.Enum.AttachFormat.Video)
                {
                    imgShare.ImageUrl = BLL.WorksShareBLL.Instance.GetVideoCover(share.AttachPath);

                    if (IsCover)
                    {
                        hlShareImage.NavigateUrl = "/Pages/ShareList.aspx";
                    }
                    else
                    {
                        hlShareImage.NavigateUrl = "/Pages/VideoPlay.aspx?videoPath=" + share.AttachPath;
                    }
                }

            }
        }
    }
}